#include "SDLImport.h"
#include <string>

//! \brief Creates a matrix from a SDL surface
//!
//! \param root A SDL surface containing the image
//! \return A matrix containing the image
//!
Pixel** createMatFromSDL (SDL_Surface* root)
{
    Pixel** mat = (Pixel**)malloc(root->h * sizeof(Pixel*));
    if(!mat) {printf("Impossible to create matrix of %d lines", root->h); return NULL;}

    SDL_LockSurface(root);
    Uint8* pixels = (Uint8*)root->pixels;
    Pixel tpx;
    int li, col;

    for (li=0;li < root->h;li++)
    {
        mat[li] = (Pixel*)malloc(root->w * sizeof(Pixel));
        if(!mat[li]) {printf("Impossible to create %dth lign", li); return NULL;}
        for (col=0;col < root->w ;col++)
        {
            tpx.r = pixels[2];
            tpx.g = pixels[1];
            tpx.b = pixels[0];
            mat[li][col] = tpx;

            pixels += 3;
        }
        pixels += (root->pitch - 3*col);
    }
    SDL_UnlockSurface(root);
    return mat;
}

const char* SDLFilenameCompliance (const char* filename)
{
    char* t = strchr(filename, 92);   //  92 =
    while(t != NULL)
    {
        *t = 47;    //  47 = /
        t = strchr(filename, 92);
    }
    return filename;
}

